import scrapy

from ScrapyObject.spiders.utils.url_utils import *

'''
scrapy crawl ydak -o ydak.json
https://159i.net/
'''


class YdakSpider(scrapy.Spider):
    name = "ydak"
    # 前缀
    prefix = 'https://'
    # 中缀
    website = '159i'
    # 后缀
    suffix = '.net'
    allowed_domains = [website + '.net']
    start_urls = [prefix + website + suffix]

    def __init__(self):
        self.i = 0

    def parse(self, response):
        content = get_data(response)
        names = response.xpath("//h2[@itemprop='name']//a/text()").extract()
        p_url = response.xpath("//div[@class='video-block']//video/@ poster").extract()
        video_url = response.xpath("//div[@class='video-block']//input/@ value").extract()
        tags = ""
        if response.url.startswith(split_joint(self.prefix + self.website + self.suffix, "/video/subtitle/")):
            tags = "中文字幕"
        elif response.url.startswith(split_joint(self.prefix + self.website + self.suffix, "/video/uncd/")):
            tags = "亞洲無碼"
        elif response.url.startswith(split_joint(self.prefix + self.website + self.suffix, "/video/madou/")):
            tags = "國產中文"
        elif response.url.startswith(split_joint(self.prefix + self.website + self.suffix, "/video/oua/")):
            tags = "歐美AV"
        elif response.url.startswith(split_joint(self.prefix + self.website + self.suffix, "/video/hmmcg/")):
            tags = "動漫"
        elif response.url.startswith(split_joint(self.prefix + self.website + self.suffix, "/video/fc2ppv/")):
            tags = "FC2 PPV"

        if len(names) and len(p_url) and len(video_url) and len(tags) != 0:
            if len(names) == len(p_url) and len(p_url) == len(video_url):
                for index in range(len(names)):
                    self.i = self.i + 1
                    yield get_video_item(id=self.i, tags=tags, url=response.url, name=names[index].strip(), pUrl=split_joint(self.prefix + self.website + self.suffix, p_url[index]), vUrl=video_url[index])
        url_list = get_url(content)
        for url in url_list:
            if url.startswith('/'):
                yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix, url), callback=self.parse)
            elif url.startswith('p'):
                if response.url.startswith(split_joint(self.prefix + self.website + self.suffix, "/video/subtitle/")):
                    yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix + "/video/subtitle/", url), callback=self.parse)
                elif response.url.startswith(split_joint(self.prefix + self.website + self.suffix, "/video/uncd/")):
                    yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix + "/video/uncd/", url), callback=self.parse)
                elif response.url.startswith(split_joint(self.prefix + self.website + self.suffix, "/video/madou/")):
                    yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix + "/video/madou/", url), callback=self.parse)
                elif response.url.startswith(split_joint(self.prefix + self.website + self.suffix, "/video/oua/")):
                    yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix + "/video/oua/", url), callback=self.parse)
                elif response.url.startswith(split_joint(self.prefix + self.website + self.suffix, "/video/hmmcg/")):
                    yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix + "/video/hmmcg/", url), callback=self.parse)
                elif response.url.startswith(split_joint(self.prefix + self.website + self.suffix, "/video/fc2ppv/")):
                    yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix + "/video/fc2ppv/", url), callback=self.parse)
